import { createRouter, createWebHistory } from 'vue-router'
import { checkAuth } from '../utils/auth'

const routes = [
  {
    path: '/',
    name: 'Welcome',
    component: () => import('../views/Welcome.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/login-code',
    name: 'LoginCode',
    component: () => import('../views/LoginCode.vue')
  },
  {
    path: '/dingtalk-login',
    name: 'DingtalkLogin',
    component: () => import('../views/DingtalkLogin.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/interest-selection',
    name: 'InterestSelection',
    component: () => import('../views/InterestSelection.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/feed',
    name: 'ContentFeed',
    component: () => import('../views/ContentFeed.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/publish',
    name: 'Publish',
    component: () => import('../views/Publish.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/dingtalk-callback',
    name: 'DingtalkCallback',
    component: () => import('../views/DingtalkLogin.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/real-name',
    name: 'RealName',
    component: () => import('../views/RealName.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/customer-service',
    name: 'CustomerService',
    component: () => import('../views/CustomerService.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/online-consultation',
    name: 'OnlineConsultation',
    component: () => import('../views/OnlineConsultation.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/human-service',
    name: 'HumanService',
    component: () => import('../views/HumanService.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/service-management',
    name: 'ServiceManagement',
    component: () => import('../views/ServiceManagement.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/content/:id',
    name: 'ContentDetail',
    component: () => import('../views/ContentDetail.vue'),
    meta: { requiresAuth: false } // 详情页可以不登录查看
  },
  {
    path: '/coze-chat',
    name: 'CozeChat',
    component: () => import('../views/CozeChat.vue'),
    meta: { requiresAuth: false } // Coze 对话可以不登录使用
  },
  {
    path: '/goods/:id/review',
    name: 'GoodsReview',
    component: () => import('../views/GoodsReview.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/Cart.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('../views/Orders.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/create-order',
    name: 'CreateOrder',
    component: () => import('../views/CreateOrder.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/order-test',
    name: 'OrderTest',
    component: () => import('../views/OrderTest.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/shop',
    name: 'Shop',
    component: () => import('../views/Shop.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/goods/:id',
    name: 'GoodsDetail',
    component: () => import('../views/GoodsDetail.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/pay',
    name: 'Pay',
    component: () => import('../views/Pay.vue'),
    meta: { requiresAuth: false }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      alert('请先登录')
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router